﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace QLHH
{
    class HoaDonDAL
    {
         public HoaDonDAL() 
        {
        
        }        
        public DataTable DocDanhSachHoaDon() 
        {
                SqlConnection conn = new SqlConnection();
                conn.ConnectionString=@"Data Source=HUNGLE-PC\SQLEXPRESS;Initial Catalog=qlvt; User Id=sa; Password=sa";
                conn.Open();
                SqlCommand cmd = new SqlCommand("select * from hoadon inner join hanghoa on hanghoa.mahang=hoadon.mahang", conn);
                SqlDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();
                dt.Load(dr);
                return dt;
        
        }
        public void XoaHoaDon(string sohd)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=HUNGLE-PC\SQLEXPRESS;Initial Catalog=qlvt; User Id=sa; Password=sa";
            conn.Open();
            SqlCommand cmd = new SqlCommand("delete from hoadon where sohd ='"+ sohd +"'", conn);
            cmd.ExecuteNonQuery();
        }
        public void ThemHoaDon(string sohoadon, string mahang, string diengiai,
                          double soluong, double dongia, double trigia, double khuyenmai)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=HUNGLE-PC\SQLEXPRESS;Initial Catalog=qlvt; User Id=sa; Password=sa";
            conn.Open();
            SqlCommand cmd = new SqlCommand("insert into hoadon values('"+ sohoadon+"','" + mahang + "',"+ soluong.ToString()+","+ dongia.ToString()+","+ trigia.ToString() + "," + khuyenmai + ",'"+diengiai+"')", conn);
            cmd.ExecuteNonQuery();
        }
        public void SuaHoaDon(string shd, string sohoadon, string mahang, string diengiai,
                         double soluong, double dongia, double trigia, double khuyenmai)
        {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = @"Data Source=HUNGLE-PC\SQLEXPRESS;Initial Catalog=qlvt; User Id=sa; Password=sa";
            conn.Open();
            SqlCommand cmd = new SqlCommand("update hoadon set sohd = '" + sohoadon + "', mahang = '" + mahang + "',soluong=" + soluong.ToString() + ",dongia=" + dongia.ToString() + ",trigia=" + trigia.ToString() + ",khuyenmai=" + khuyenmai + ",diengiai='" + diengiai + "' where sohd ='" + shd +"'", conn);
            cmd.ExecuteNonQuery();
        }

    
    }
}
